const unsigned char configbmp[] U8X8_PROGMEM {
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0x07, 0x00, 0x9e, 0x07, 0x18, 0x78, 0x00, 0xe0, 0x07, 0x00, 0x9e, 0x07,
  0x18, 0x78, 0x00, 0xe0, 0x07, 0x12, 0x7e, 0x80, 0x79, 0x60, 0x20, 0xe1,
  0xe7, 0x7f, 0x7e, 0x80, 0x79, 0x60, 0xfe, 0xe7, 0x47, 0x40, 0x06, 0xe6,
  0xf9, 0x61, 0x04, 0xe4, 0x47, 0x40, 0x06, 0xe6, 0xf9, 0x61, 0x04, 0xe4,
  0x47, 0x40, 0x1e, 0xf8, 0x79, 0x78, 0x04, 0xe4, 0x67, 0x60, 0x1e, 0xf8,
  0x79, 0x78, 0x06, 0xe6, 0x47, 0x40, 0x7e, 0x1e, 0x18, 0x78, 0x04, 0xe4,
  0x47, 0x40, 0x7e, 0x1e, 0x18, 0x78, 0x04, 0xe4, 0x47, 0x69, 0x9e, 0x7f,
  0x00, 0x78, 0x94, 0xe6, 0xe7, 0x7f, 0x9e, 0x7f, 0x00, 0x78, 0xfe, 0xe7,
  0x07, 0x00, 0x66, 0x66, 0x66, 0x66, 0x00, 0xe0, 0x07, 0x00, 0x66, 0x66,
  0x66, 0x66, 0x00, 0xe0, 0xff, 0xff, 0x07, 0x06, 0x18, 0xe6, 0xff, 0xff,
  0xff, 0xff, 0x07, 0x06, 0x18, 0xe6, 0xff, 0xff, 0x07, 0x06, 0x60, 0x98,
  0x01, 0x00, 0x7e, 0xfe, 0x07, 0x06, 0x60, 0x98, 0x01, 0x00, 0x7e, 0xfe,
  0x9f, 0xff, 0x67, 0x98, 0xe7, 0x1f, 0x98, 0xe7, 0x9f, 0xff, 0x67, 0x98,
  0xe7, 0x1f, 0x98, 0xe7, 0x67, 0x7e, 0x98, 0x1f, 0x9e, 0x87, 0x61, 0xe0,
  0x67, 0x7e, 0x98, 0x1f, 0x9e, 0x87, 0x61, 0xe0, 0x67, 0xfe, 0xe1, 0x19,
  0x06, 0xfe, 0x81, 0xf9, 0x67, 0xfe, 0xe1, 0x19, 0x06, 0xfe, 0x81, 0xf9,
  0x07, 0x06, 0xfe, 0x87, 0x9f, 0x1f, 0x9e, 0xe1, 0x07, 0x06, 0xfe, 0x87,
  0x9f, 0x1f, 0x9e, 0xe1, 0xff, 0x81, 0xe7, 0xe1, 0x87, 0x67, 0x1e, 0xe6,
  0xff, 0x81, 0xe7, 0xe1, 0x87, 0x67, 0x1e, 0xe6, 0xff, 0x61, 0x66, 0x80,
  0x7f, 0x1e, 0xe0, 0xe1, 0xff, 0x61, 0x66, 0x80, 0x7f, 0x1e, 0xe0, 0xe1,
  0x07, 0xfe, 0xe7, 0x07, 0x06, 0x80, 0x99, 0xf9, 0x07, 0xfe, 0xe7, 0x07,
  0x06, 0x80, 0x99, 0xf9, 0xe7, 0x01, 0x9e, 0x99, 0x81, 0x07, 0xfe, 0xe1,
  0xe7, 0x01, 0x9e, 0x99, 0x81, 0x07, 0xfe, 0xe1, 0xff, 0x99, 0xe7, 0xf9,
  0x1f, 0x06, 0x7e, 0xe6, 0xff, 0x99, 0xe7, 0xf9, 0x1f, 0x06, 0x7e, 0xe6,
  0xe7, 0x01, 0x9e, 0x9f, 0x67, 0xfe, 0x9f, 0xe1, 0xe7, 0x01, 0x9e, 0x9f,
  0x67, 0xfe, 0x9f, 0xe1, 0x9f, 0x99, 0x81, 0x99, 0x81, 0x9f, 0x87, 0xf9,
  0x9f, 0x99, 0x81, 0x99, 0x81, 0x9f, 0x87, 0xf9, 0x67, 0x1e, 0x9e, 0x87,
  0x87, 0x01, 0xe0, 0xff, 0x67, 0x1e, 0x9e, 0x87, 0x87, 0x01, 0xe0, 0xff,
  0xff, 0xff, 0x87, 0x81, 0xe7, 0xe1, 0x67, 0xe0, 0xff, 0xff, 0x87, 0x81,
  0xe7, 0xe1, 0x67, 0xe0, 0x07, 0x00, 0x66, 0x80, 0x67, 0x60, 0x86, 0xe1,
  0x07, 0x00, 0x66, 0x80, 0x67, 0x60, 0x86, 0xe1, 0x07, 0x12, 0xe6, 0x87,
  0x01, 0xe6, 0xe7, 0xf9, 0xe7, 0x7f, 0xe6, 0x87, 0x01, 0xe6, 0xe7, 0xf9,
  0x47, 0x40, 0x66, 0x80, 0x01, 0x06, 0x80, 0xf9, 0x47, 0x40, 0x66, 0x80,
  0x01, 0x06, 0x80, 0xf9, 0x47, 0x40, 0x7e, 0xf8, 0xe7, 0xe1, 0xe1, 0xe1,
  0x67, 0x60, 0x7e, 0xf8, 0xe7, 0xe1, 0xe1, 0xe1, 0x47, 0x40, 0x66, 0x7e,
  0x9e, 0xf9, 0x87, 0xe7, 0x47, 0x40, 0x66, 0x7e, 0x9e, 0xf9, 0x87, 0xe7,
  0x47, 0x69, 0x06, 0x06, 0x06, 0x00, 0xf8, 0xf9, 0xe7, 0x7f, 0x06, 0x06,
  0x06, 0x00, 0xf8, 0xf9, 0x07, 0x00, 0x86, 0x87, 0x81, 0x01, 0x9e, 0xe1,
  0x07, 0x00, 0x86, 0x87, 0x81, 0x01, 0x9e, 0xe1, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};

void wifiConfig() {
  if (WiFi.status() == WL_CONNECTED) {
    String ipaddress = WiFi.localIP().toString();
    u8g2.userInterfaceMessage("Connected", "IP Addr:", ipaddress.c_str(), " OK ");
  } else {
    smartConfig();
    Serial.println("SmartConfig Success");
    Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
    Serial.printf("Password:%s\r\n", WiFi.psk().c_str());
    writeConfig(WiFi.SSID(), WiFi.psk());
    int selection = u8g2.userInterfaceMessage("Connected", "To:", WiFi.SSID().c_str(), " OK \n Disconnect ");
    if (selection == 2) {
      WiFi.disconnect(true);
    }
  }
}

/*
 * Smart Config.
 * This function comes from Cloud_1234_5678@blog.csdn.net.
 * Reference: https://blog.csdn.net/Cloud_1234_5678/article/details/84387716
 */

void smartConfig() {
  Serial.println("Wait for smartconfig...");
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_ncenB10_tr);
  u8g2.drawStr(0, 12, "Scan");
  u8g2.drawStr(0, 24, "to");
  u8g2.drawStr(0, 36, "config");
  u8g2.drawXBMP(64, 0, 64, 64, configbmp);
  u8g2.sendBuffer();
  WiFi.beginSmartConfig();
  while (true) {
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(1000);                       // wait for a second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(1000);                       // wait for a second
    btnenter.tick();
    if (WiFi.smartConfigDone() || btnenterClicked) {
      btnenterClicked = false;
      break;
    }
    yield();
  }
}

void connectfromconfig() {
  String ssid = readStringFromEEPROM(0);
  String passwd = readStringFromEEPROM(64);
  Serial.print("SSID from EEPROM: ");
  Serial.println(ssid);
  Serial.println("Password from EEPROM: ");
  Serial.println(passwd);
  WiFi.begin(ssid, passwd);
}

void writeConfig(String ssid, String passwd) {
  writeStringToEEPROM(0, ssid);
  writeStringToEEPROM(64, passwd);
}
